 /*
 * @name: 分类列表
 * @Author: along
 * @Date: 2019-07-31
 * @Last Modified by: along
 * @Last Modified time: 2020-07-31
 */
<template>
    <div class="container question">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">搜索问题:</p>
                    <el-input
                        v-model="filterData.keyword"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        搜索
                    </el-button>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnAddCate()"
                    >
                        添加分类
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="question"
        >
            <el-table-column
                label="序号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column
                prop="name"
                label="分类名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="sort"
                label="排序"
                align="left"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="目标"
                align="left"
            >
                <template slot-scope="scope">
                    <span
                        v-for="(item, index) in scope.row.scope"
                        :key="index"
                    >
                        {{ item === 1 ? '商家后台' : '学生后台' }}
                        <span v-if="scope.row.scope.length === 2 && index === 0">，</span>
                    </span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEdiyCate(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnRemoveCate(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <el-dialog
            title="添加分类"
            :visible.sync="dialogVisible"
            width="618px"
            :close-on-click-modal="false"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
                :label-position="'right'"
            >
                <el-form-item
                    label="目标:"
                    prop="scope"
                >
                    <el-checkbox-group v-model="ruleForm.scope">
                        <el-checkbox :label="1">商家后台</el-checkbox>
                        <el-checkbox :label="2">学生后台</el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item
                    label="排序:"
                    prop="sort"
                >
                    <el-input
                        v-model="ruleForm.sort"
                        size="small"
                        placeholder="请输入排序号"
                    />
                </el-form-item>
                <el-form-item
                    label="分类名称:"
                    prop="sort"
                >
                    <el-input
                        v-model="ruleForm.name"
                        size="small"
                        placeholder="请输入分类名称"
                        maxlength="12"
                        show-word-limit
                    />
                </el-form-item>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="small"
                    :style="{width: '100px', height: '32px'}"
                >
                    取 消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    :style="{width: '100px', height: '32px', marginLeft: '60px'}"
                    @click="fnChangeCate('ruleForm')"
                >
                    确 定
                </el-button>
            </span>
        </el-dialog>

        <!-- 删除分类 -->
        <div
            class="fixed"
            v-if="DIALOG"
        >
            <div class="wrap">
                <i
                    class="el-icon-close"
                    @click="DIALOG = false"
                />
                <img
                    src="@/assets/image/zd-jg.png"
                    class="wrap-logo"
                >
                <p class="wrap-title">
                    是否删除
                </p>
                <p class="wrap-tip">
                    当前分类下含有子问题，确定一起删除吗？
                </p>
                <div class="wrap-foot">
                    <el-button
                        size="small"
                        type="default"
                        :style="{width: '100px', height: '32px'}"
                        @click="DIALOG = false"
                    >
                        取消
                    </el-button>
                    <el-button
                        size="small"
                        type="primary"
                        :style="{width: '100px', height: '32px', marginLeft: '40px'}"
                        @click="fnConfirmDelete()"
                    >
                        确定
                    </el-button>
                </div>
            </div>
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            ruleForm: {
                scope: [1, 2],
                name: '',
                sort: ''
            },
            rules: {
                name: [
                    { required: true, message: '请填写分类名称', trigger: 'change' }
                ],
                sort: [
                    { required: true, message: '请填写排序号', trigger: 'change' }
                ],
                scope: [
                    { required: true, message: '请勾选目标', trigger: 'change' }
                ]
            },
            dialogVisible: false,
            filterData: {
                keyword: ''
            },
            tableData: [],
            authResult: {},
            type: '',
            cate_no: '',
            DIALOG: false,
            rowCate: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        dialogVisible (val) {
            if(!val) {
                this.ruleForm = {
                    sort: '',
                    name: '',
                    scope: [1, 2]
                };
                this.$refs['ruleForm'].resetFields();
            }
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "官网"},{title: '帮助中心管理'},{title: '分类列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData () {
            this.$post('/student/hire_help%5Cget_cate_list', {
                keyword: this.filterData.keyword
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                }
            });
        },

        /**
         * @description 添加/编辑分类
         */
        fnChangeCate (formName) {
            if(this.type == 'add') {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$post('/student/hire_help%5Cadd_cate',{
                            name: this.ruleForm.name,
                            sort: this.ruleForm.sort,
                            scope: this.ruleForm.scope.join()
                        },resp=>{
                            if (resp.code == 1) {
                                this.dialogVisible = !this.dialogVisible;
                                this.getTableData();
                                this.$notify({
                                    title: '成功',
                                    message: '添加成功',
                                    type: 'success',
                                    duration: 2000
                                });
                            } else if (resp.code == 0) {
                                this.$notify({
                                    title: '提示',
                                    message: resp.msg,
                                    type: 'warning',
                                    duration: 2000
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            } else if (this.type == 'edit') {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$post('/student/hire_help%5Cset_cate',{
                            cate_no: this.cate_no,
                            name: this.ruleForm.name,
                            sort: this.ruleForm.sort,
                            scope: this.ruleForm.scope.join()
                        },resp=>{
                            if (resp.code == 1) {
                                this.dialogVisible = !this.dialogVisible;
                                this.getTableData();
                                this.$notify({
                                    title: '成功',
                                    message: '编辑成功',
                                    type: 'success',
                                    duration: 2000
                                });
                            } else if (resp.code == 0) {
                                this.$notify({
                                    title: '提示',
                                    message: resp.msg,
                                    type: 'warning',
                                    duration: 2000
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            } else {
                return false;
            }
        },

        /**
         * @description 添加分类
         */
        fnAddCate () {
            this.type = 'add';
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * @description 编辑分类
         */
        fnEdiyCate (row) {
            this.cate_no = row.cate_no;
            this.type = 'edit';
            this.ruleForm = {
                name: row.name,
                sort: row.sort,
                scope: row.scope
            };
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/hire_help%5Cset_cate',{
                    cate_no: row.cate_no,
                    sort: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 删除分类
         */
        fnRemoveCate (row) {
            this.getList(row.cate_no).then(resp => {
                if(resp && resp.length > 0) {
                    this.rowCate = row;
                    this.DIALOG = !this.DIALOG;
                } else {
                    this.fnDeleteCate(row);
                }
            });
        },

        /**
         * @description 确定删除分类
         */
        fnConfirmDelete () {
            this.$post('/student/hire_help%5Cdelete_cate',{
                cate_no: this.rowCate.cate_no
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.DIALOG = !this.DIALOG;
                    this.$notify({
                        title: '成功',
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 删除分类函数
         */
        fnDeleteCate (row) {
            this.$confirm('是否要删除此分类', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/hire_help%5Cdelete_cate',{
                    cate_no: row.cate_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '成功',
                            message: '删除成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 获取帮助列表，用于删除判断分类下是否有问题
         */
        getList (cate_no, callback) {
            return new Promise((resolve,reject) => {
                this.$post('/student/hire_help%5Cget_list', {
                    cate_no: cate_no
                }, resp => {
                    if (resp.code == 1) {
                        resolve(resp.data.list);
                    } else {
                        reject();
                    }
                });
            });
        },

        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        text-align: left;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .dialog-footer {
        display: flex;
        height: 32px;
        justify-content: center;
        margin-bottom: 15px;
    }
    .fixed {
        position: fixed;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto;
        background: rgba(0,0,0,0.6);
        z-index: 10008;
        display: flex;
        justify-content: center;
        align-items: center;
        .wrap {
            width:360px;
            height:258px;
            background:rgba(255,255,255,1);
            border-radius:4px;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            align-items: center;
            .el-icon-close {
                color: #C0CCDA;
                font-size: 18px;
                font-weight: 500;
                margin-left: 325px;
                cursor: pointer;
                margin-top: 20px;
            }
            .wrap-logo {
                width: 48px;
                height: 48px;
                border-radius: 50%;
                margin-bottom: 12px;
            }
            .wrap-title {
                color: #333333;
                font-size: 16px;
                font-weight: 500;
            }
            .wrap-tip {
                color: #666;
                font-size: 14px;
                margin: 24px 0 48px 0;
            }
            .wrap-foot {
                width: 100%;
                height: 32px;
                display: flex;
                justify-content: center;
            }
        }
    }
</style>
<style lang="less">
    .question{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .el-dialog__body {
            padding: 32px 85px 0 62px;
        }
    }
</style>
